package com.hmy.park.controller;

import com.hmy.park.domain.HmyPhouse;
import com.hmy.park.mapper.PhouseMapper;
import com.hmy.park.service.PhouseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author ljy
 * @Date 2024/1/30 17:44
 * @description: 接口
 * @Title: PhouseController
 * @Package com.hmy.park.controller
 */
@RestController
@RequestMapping("/phouse")
public class PhouseController extends BaseController {
    @Resource
    private PhouseService phouseService;
    @Resource
    private PhouseMapper phouseMapper;

    /**
     * @param hmyPhouse
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 多条件查询
     * @author ljy
     * @date 2024/1/30 19:01:21
     */
    @GetMapping("/find")
    public TableDataInfo findlist(HmyPhouse hmyPhouse) {
        startPage();
        List<HmyPhouse> phouseList = phouseService.findPhouseList(hmyPhouse);
        return getDataTable(phouseList);
    }

    /**
     * @param phouseId
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据id查询
     * @author ljy
     * @date 2024/1/31 18:47:07
     */
    @GetMapping("/getPhouse/{phouseId}")
    public AjaxResult getPhouse(@PathVariable(value = "phouseId") Long phouseId) {
        HmyPhouse phouseById = phouseMapper.findPhouseById(phouseId);
        return AjaxResult.success(phouseById);
    }

    /**
     * @param phouseCode
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据id查询
     * @author ljy
     * @date 2024/1/31 18:47:07
     */
    @GetMapping("/findCoud/{phouseCode}")
    public AjaxResult findPhouseByCoud(@PathVariable(value = "phouseCode") String phouseCode) {
        HmyPhouse phouseByCoud = phouseMapper.findPhouseByCoud(phouseCode);
        return AjaxResult.success(phouseByCoud);
    }

    /**
     * @param hmyPhouse
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 添加栏舍
     * @author ljy
     * @date 2024/1/29 19:30:40
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HmyPhouse hmyPhouse) {
        hmyPhouse.setCreatedBy(SecurityUtils.getUsername());
        Integer i = phouseService.savePhouse(hmyPhouse);
        return toAjax(i);
    }

    /**
     * @param hmyPhouse
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 修改栏舍
     * @author ljy
     * @date 2024/1/29 19:46:41
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HmyPhouse hmyPhouse) {
        String userName = SecurityUtils.getUsername();
        hmyPhouse.setUpdatedBy(userName);
        Date date = new Date();
        hmyPhouse.setUpdationDate(date);
        Integer i = phouseService.updatePhouse(hmyPhouse);
        return toAjax(i);
    }

    /**
     * @param phouseId
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据id删除
     * @author ljy
     * @date 2024/1/29 19:52:59
     */
    @PostMapping("/del/{phouseId}")
    public AjaxResult delByid(@PathVariable(value = "phouseId") Long phouseId[]) {
        Integer i = phouseService.delPhouse(phouseId);
        return toAjax(i);
    }
}
